import xlrd

class Nvm_Excel(object):

    def __init__(self):
        print('Nvm_Sheet init...')

    def NvmExcelRead(self, filepath, sheetindex):
        readbook = xlrd.open_workbook(filepath)
        self.sheet = readbook.sheet_by_index(sheetindex)
        self.mergedata = self.sheet.merged_cells # (statrwo, endrow, startclo, endclo)
        # print(self.mergedata)

    def _NvmExcelGetMergeData(self, row, clo):
        for (startrow, endrow, startclo, endclo) in self.mergedata:  # 遍历表格中所有合并单元格
            if (startrow <= row < endrow and startclo <= clo < endclo):
                return str(self.sheet.cell(startrow, startclo).value)
        return ''

    def NvmExcelGetData(self, row, col):
        celltmp = self.sheet.cell(row, col)
        if (celltmp.value != None and celltmp.value != ""):
            return str(celltmp.value)
        return self._NvmExcelGetMergeData(row, col)
    
    def NvmExcelGetRows(self):
        return self.sheet.nrows
    
    def NvmExcelGetCols(self):
        return self.sheet.ncols
